package com.chu.service.impl;

import com.chu.entity.Employee;
import com.chu.entity.Menu;
import com.chu.entity.Role;
import com.chu.mapper.EmployeeMapper;
import com.chu.mapper.MenuMapper;
import com.chu.mapper.RoleMapper;
import com.chu.service.EmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper mapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private MenuMapper menuMapper;


    //根据导出的id查询需要序列化的对象
    @Override
    public List<Employee> findAllByIds(int[] id) {
        return mapper.findAllByIds(id);
    }
    //根据账号查询用户做登录
    @Override
    public Employee loginByLoginName(String loginName,String loginPassword) {

        Employee employee = mapper.loginByLoginName(loginName);

        if(employee!=null){
            //如果用户名正确
            if(employee.getLoginpassword().equals(loginPassword)){
                //查询用户对应的角色列表
                Role role = roleMapper.selectByPrimaryKey(employee.getRoleid());
                //根据角色信息查询角色权限
                List<Menu> menus = menuMapper.findMenusByRoleId(role.getRoleid());
                //对菜单列表分级
                //创建集合保存一级菜单
                ArrayList firstList = new ArrayList();
                for (Menu menu : menus) {
                    if(menu.getUpmenuid()==0){//一级菜单
                        ArrayList secondList = new ArrayList();//创建集合保存二级菜单
                        for (Menu secondMenu : menus) {
                            if(menu.getMenuid()==secondMenu.getUpmenuid()){//如果菜单的父id等于一级菜单的id 二级菜单
                                //保存二级菜单
                                secondList.add(secondMenu);
                            }
                        }
                        //将二级菜单保存到一级菜单当中
                        menu.setMenuList(secondList);
                        //将一级菜单添加到集合当中
                        firstList.add(menu);
                    }
                }
                //将权限集合交给角色
                role.setMenuList(firstList);
                //保存角色对应的请求
                List<Menu> permissionList = menuMapper.findPermissionByRoleId(role.getRoleid());
                role.setPermissionList(permissionList);
                //将角色交给用户
                employee.setRole(role);
                return employee;

            }else{
                return null;
            }
        }else{
            return null;
        }
    }

    //根据账号查询用户
    @Override
    public Employee findByLoginName(String loginName) {

        return mapper.findByLoginName(loginName);
    }

    //查询所有分页
    @Override
    public PageInfo<Employee> findAll(int pageIndex,int pageSize,String username,String rolename,String deptname) {
        PageHelper.startPage(pageIndex, pageSize);

        Map map = new HashMap();
        map.put("uname",username);
        map.put("rname",rolename);
        map.put("dname",deptname);
        List<Employee> list = mapper.findAll(map);
        PageInfo<Employee> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
    //批量假删除
    @Override
    @Transactional
    public int deleteBatch(int[] params) {
        return mapper.deleteBatch(params);
    }
    //假删除
    @Override
    @Transactional
    public int deleteByPrimaryKey(Integer id,int state) {
        Map map = new HashMap();
        map.put("did",id);
        map.put("state",state);
        return mapper.deleteByPrimaryKey(map);
    }

    @Override
    @Transactional
    public int insert(Employee record) {
        return mapper.insert(record);
    }

    @Override
    public int insertSelective(Employee record) {
        return 0;
    }

    @Override
    public Employee selectByPrimaryKey(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Employee record) {
         return mapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Employee record) {
        return 0;
    }
}
